#include "mainwindow.h"
#include "homepage.h"
#include "public.h"
#include "ui_mainwindow.h"
#include "windowabout.h"
#include "windowhexconvert.h"

#include <QDebug>
#include <QMdiSubWindow>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QString windowTitleName = QString("%1 V%2").arg(APPLICATION_NAME).arg(APPLICATION_VERSION);
    setWindowTitle(windowTitleName);

    ui->mdiArea->setViewMode(QMdiArea::TabbedView);
    ui->mdiArea->setTabsClosable(true);
    ui->mdiArea->setTabsMovable(true);

    homePage = new HomePage(this);
    ui->mdiArea->addSubWindow(homePage);
    homePage->setAttribute(Qt::WA_DeleteOnClose);
    homePage->showMaximized();

    connect(homePage, &HomePage::captionClicked, this, &MainWindow::onCaptionClicked);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionFind_triggered() {}

void MainWindow::on_actionTimeFormat_triggered() {}

void MainWindow::on_actionHomePage_triggered()
{
    auto windows = ui->mdiArea->subWindowList();
    bool alreadyExistHomePage = false;
    for (const auto &w : windows) {
        QWidget *internalWidget = w->widget();
        HomePage *homePage = dynamic_cast<HomePage *>(internalWidget);
        if (homePage != nullptr) {
            alreadyExistHomePage = true;
            ui->mdiArea->setActiveSubWindow(w);
            break;
        }
    }

    if (!alreadyExistHomePage) {
        HomePage *homePage = new HomePage(this);
        homePage->setAttribute(Qt::WA_DeleteOnClose);
        auto w = ui->mdiArea->addSubWindow(homePage);
        ui->mdiArea->setActiveSubWindow(w);

        homePage->showMaximized();
    }
}

void MainWindow::on_actionNext_triggered()
{
    ui->mdiArea->activateNextSubWindow();
}

void MainWindow::on_actionPrevious_triggered()
{
    ui->mdiArea->activatePreviousSubWindow();
}

void MainWindow::on_actionCloseAll_triggered()
{
    ui->mdiArea->closeAllSubWindows();
}

void MainWindow::on_actionCascade_triggered()
{
    ui->mdiArea->cascadeSubWindows();
}

void MainWindow::on_actionTileHorizontally_triggered()
{
    ui->mdiArea->tileSubWindows();
}

void MainWindow::on_actionAlwaysOnTop_toggled(bool flag)
{
    if (flag) {
        setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
        show();
    } else {
        setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
        show();
    }
}

void MainWindow::on_actionTransparent_toggled(bool flag)
{
    if (flag)
        setWindowOpacity(0.5);
    else
        setWindowOpacity(1);
}

void MainWindow::on_actionAbout_triggered()
{
    WindowAbout *aboutWindow = new WindowAbout;
    aboutWindow->setAttribute(Qt::WA_DeleteOnClose);
    aboutWindow->show();
}

void MainWindow::onCaptionClicked(ToolType toolType)
{
    switch (toolType) {
    case ToolType::TOOL_HEX_CONVERTER: {
        WindowHexConvert *w = new WindowHexConvert(this);
        connect(w,
                &WindowHexConvert::showMessageInStatusBar,
                this,
                &MainWindow::onShowMessageInStatusBar);
        ui->mdiArea->addSubWindow(w);
        w->showMaximized();
    } break;

    case ToolType::TOOL_SERIAL_PORT_TOOL:
        break;
    case ToolType::TOOL_TCP_CLIENT:
        break;
    case ToolType::TOOL_TCP_SERVER:
        break;
    case ToolType::TOOL_UDP_CLIENT:
        break;
    case ToolType::TOOL_UDP_SERVER:
        break;
    case ToolType::TOOL_NANZI103_SERVER:
        break;
    case ToolType::TOOL_NANZI103_CLIENT:
        break;
    case ToolType::TOOL_NANZI103_PROTOCOL_PARSER:
        break;
    case ToolType::TOOL_SERIALPORT103_PROTOCOL_PARSER:
        break;
    }
}

void MainWindow::onShowMessageInStatusBar(const QString &msg)
{
    auto bar = statusBar();
    bar->showMessage(msg, 3000);
}
